<?php
	session_start();
?>
<script src="http://www.wowhead.com/widgets/power.js"></script>
<?php
include 'include.php';

	mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die(mysql_error());
	mysql_select_db(DB_NAME);
	$date_limit = time() - ($_GET["months"] * 2592000);
	if($_GET["loots_type"] == "false") {
		$sql = "SELECT * from loots join players on loots.players_id = players.players_id WHERE loots.loots_type = 0 AND loots.loots_date > " . $date_limit . " ORDER BY players.players_name,loots.loots_date;";
	}
	else {
		$sql = "SELECT * from loots join players on loots.players_id = players.players_id WHERE loots.loots_date > " . $date_limit . " ORDER BY players.players_name,loots.loots_date;";
	}
	$result = mysql_query($sql);
	$current_player = -99;
	$total_loots = 0;
	$last_loot = -99;
	$last_loot_date = -99;
	$loots_array = array();
	$player_filter = explode(";",$_GET["player_filter"]);
	for($x=0;$x<sizeof($player_filter);$x++) {
		$player_filter[$x] = strtolower($player_filter[$x]);
	}
	if($_GET["show_all"] == "false") {
		while($myrow = mysql_fetch_row($result)) {
			if($myrow[1] != $current_player) {
				if($current_player != -99) {
					if(in_array(get_player_name($current_player), $player_filter) || $player_filter[0] == "all") {
						$loot_data_array = array(
							0=>$current_player,
							1=>$total_loots,
							2=>$last_loot,
							3=>$last_loot_date,
							4=>$loot_types);
						$loots_array[] = $loot_data_array;
					}
				}
				$total_loots = 0;
			}
			$total_loots++;
			$last_loot = $myrow[3];
			$last_loot_date = $myrow[2];
			$current_player = $myrow[1];
			$loot_types = $myrow[4];
		}
		if($current_player != -99) {
			if(in_array(get_player_name($current_player), $player_filter) || $player_filter[0] == "all") {
				$loot_data_array = array(
					0=>$current_player,
					1=>$total_loots,
					2=>$last_loot,
					3=>$last_loot_date,
					4=>$loot_types);
				$loots_array[] = $loot_data_array;
			}
		}
	}
	else {
		while($myrow = mysql_fetch_row($result)) {
			$last_loot = $myrow[3];
			$last_loot_date = $myrow[2];
			$current_player = $myrow[1];
			$total_loots = -99;
			$loot_types = $myrow[4];
			if(in_array(get_player_name($current_player), $player_filter) || $player_filter[0] == "all") {
				$loot_data_array = array(
					0=>$current_player,
					1=>$total_loots,
					2=>$last_loot,
					3=>$last_loot_date,
					4=>$loot_types);
				$loots_array[] = $loot_data_array;
			}
		}
	}
	$exchange = 1;
	$count = sizeof($loots_array);
	$sort_index = $_GET["sort_index"];
	if($sort_index != 0) {
		if($sort_index < 0) {
			$sort_index = $sort_index * -1;
			while($exchange) {
				$exchange = 0;
				for($a=$count-1;$a>0;--$a) {
					if($loots_array[$a-1][$sort_index] < $loots_array[$a][$sort_index]) {
						$t = $loots_array[$a-1];
						$loots_array[$a-1] = $loots_array[$a];
						$loots_array[$a] = $t;
						$exchange = 1;
					}
				}
				for($a=1;$a<$count;++$a) {
					if($loots_array[$a-1][$sort_index] < $loots_array[$a][$sort_index]) {
						$t = $loots_array[$a-1];
						$loots_array[$a-1] = $loots_array[$a];
						$loots_array[$a] = $t;
						$exchange = 1;
					}
				}
			}
		}
		else {
			while($exchange) {
				$exchange = 0;
				for($a=$count-1;$a>0;--$a) {
					if($loots_array[$a-1][$sort_index] > $loots_array[$a][$sort_index]) {
						$t = $loots_array[$a-1];
						$loots_array[$a-1] = $loots_array[$a];
						$loots_array[$a] = $t;
						$exchange = 1;
					}
				}
				for($a=1;$a<$count;++$a) {
					if($loots_array[$a-1][$sort_index] > $loots_array[$a][$sort_index]) {
						$t = $loots_array[$a-1];
						$loots_array[$a-1] = $loots_array[$a];
						$loots_array[$a] = $t;
						$exchange = 1;
					}
				}
			}
		}
	}
	echo "<table class=\"loots\">\n";
	echo "	<tr>\n";
	echo "		<th>Player<img src=\"down_arrow.png\" onclick=\"ajaxFunction(0)\"></th>\n";
if($_GET["show_all"] == "false") {
	echo "		<th>Total<img src=\"up_arrow.png\" onclick=\"ajaxFunction(-1)\"><img src=\"down_arrow.png\" onclick=\"ajaxFunction(1)\"></th>\n";
}
	echo "		<th>Last Loot</th>\n";
if($_GET["loots_type"] == "true") {
	echo "		<th>Loot Type<img src=\"up_arrow.png\" onclick=\"ajaxFunction(-4)\"><img src=\"down_arrow.png\" onclick=\"ajaxFunction(4)\"></th>\n";
}
	echo "		<th>Date<img src=\"up_arrow.png\" onclick=\"ajaxFunction(-3)\"><img src=\"down_arrow.png\" onclick=\"ajaxFunction(3)\"></th>\n";
	echo "	</tr>\n";
	for($x=0;$x<sizeof($loots_array);$x++) {
		echo "	<tr>\n";
		echo "		<td><a href=\"player_detail.php?players_id=" . $loots_array[$x][0] . "\">" . ucwords(get_player_name($loots_array[$x][0])) . "</a></td>\n";
if($_GET["show_all"] == "false") {
		echo "		<td class=\"total_loots\">" . $loots_array[$x][1] . "</td>\n";
}
		echo "		<td><a href=\"http://www.wowhead.com/?item=" . $loots_array[$x][2] . "\">" . get_item_name($loots_array[$x][2]) . "</a></td>\n";
if($_GET["loots_type"] == "true") {
	if($loots_array[$x][4] == 0) {
		echo "		<td>Normal</td>\n";
	}
	else {
		echo "		<td>Rot</td>\n";
	}
}
		echo "		<td>" . date("M j Y h:i:s A",$loots_array[$x][3]) . "</td>\n";
		echo "	</tr>\n";
	}
	echo "</table>\n";
?>
